package ap.project.controller.main;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class VotingActivity extends Activity{
	private int game1;
	private int game2;
	private int game3;
	public static Activity handleToClose;
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.voting);
		
		handleToClose = this;
		
		Bundle extras = this.getIntent().getExtras();
		if (extras != null) {
			showVoteScreen();
			game1 = extras.getInt("game1");
			game2 = extras.getInt("game2");
			game3 = extras.getInt("game3");
			setOnClickListeners();
			setGamePictures();
		}
	}

	/**
	 * Set up ClickListerners to check the player his Vote.
	 */
	private void setOnClickListeners() {
		ImageView imgGame1 = (ImageView) findViewById(R.id.game1);
		imgGame1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				finishVote(game1);
			}
		});

		ImageView imgGame2 = (ImageView) findViewById(R.id.game2);
		imgGame2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				finishVote(game2);
			}
		});
		
		ImageView imgGame3 = (ImageView) findViewById(R.id.game3);
		imgGame3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				finishVote(game3);
			}
		});
	}

	/**
	 * Send the vote to the gameserver and finish the vote activity
	 * @param game
	 */
	public void finishVote(int game) {
		JSONObject jOb = new JSONObject();

		try {
			jOb.put("Code", Constants.CODEVOTE);
			jOb.put("Vote", game);
			Connection.getInstance().send(jOb);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		showLoadingScreen();
	}

	/**
	 * Show voring screen with given games.
	 */
	private void showVoteScreen() {
		ImageView imgGame1 = (ImageView) findViewById(R.id.game1);
		imgGame1.setVisibility(View.VISIBLE);
		
		ImageView imgGame2 = (ImageView) findViewById(R.id.game2);
		imgGame2.setVisibility(View.VISIBLE);
		
		ImageView imgGame3 = (ImageView) findViewById(R.id.game3);
		imgGame3.setVisibility(View.VISIBLE);
		
		TextView txtVoteTitle = (TextView) findViewById(R.id.txtGameVoteTitle);
		txtVoteTitle.setVisibility(View.VISIBLE);
		
		ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar);
		pb.setVisibility(View.INVISIBLE);
		
		TextView txtLoadingScreen = (TextView) findViewById(R.id.txtGameLoading);
		txtLoadingScreen.setVisibility(View.INVISIBLE);
	}

	/**
	 * Show loading screen based on gamestate
	 */
	private void showLoadingScreen() {
		ImageView imgGame1 = (ImageView) findViewById(R.id.game1);
		imgGame1.setVisibility(View.INVISIBLE);
		
		ImageView imgGame2 = (ImageView) findViewById(R.id.game2);
		imgGame2.setVisibility(View.INVISIBLE);
		
		ImageView imgGame3 = (ImageView) findViewById(R.id.game3);
		imgGame3.setVisibility(View.INVISIBLE);
		
		TextView txtVoteTitle = (TextView) findViewById(R.id.txtGameVoteTitle);
		txtVoteTitle.setVisibility(View.INVISIBLE);
		
		ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar);
		pb.setVisibility(View.VISIBLE);
		
		TextView txtLoadingScreen = (TextView) findViewById(R.id.txtGameLoading);
		txtLoadingScreen.setVisibility(View.VISIBLE);
		
	}

	/**
	 * Setup game pictures for voting.
	 */
	public void setGamePictures() {
		ImageView imgGame1 = (ImageView) findViewById(R.id.game1);
		imgGame1.setImageDrawable(getGameImage(game1));
		ImageView imgGame2 = (ImageView) findViewById(R.id.game2);
		imgGame2.setImageDrawable(getGameImage(game2));
		ImageView imgGame3 = (ImageView) findViewById(R.id.game3);
		imgGame3.setImageDrawable(getGameImage(game3));
	}

	/**
	 * Setting the games to vote on based on the received gamelist from the gameserver.
	 * @param game
	 * @return image of game
	 */
	private Drawable getGameImage(int game) {
		Drawable gameImage = getApplicationContext().getResources()
				.getDrawable(R.drawable.placeholder);
		switch (game) {
		case 1:
			gameImage = getApplicationContext().getResources().getDrawable(
					R.drawable.game1);
			break;

		case 2:
			gameImage = getApplicationContext().getResources().getDrawable(
					R.drawable.game2);
			break;

		case 3:
			gameImage = getApplicationContext().getResources().getDrawable(
					R.drawable.game3);
			break;

		default:
			gameImage = getApplicationContext().getResources().getDrawable(
					R.drawable.placeholder);
			break;
		}
		return gameImage;
	}
}
